//
//  ChatExtDB.m
//  KoalaKnow
//  
//  Created by sally on 16/1/22.
//  Copyright © 2016年 szjn. All rights reserved.
//

#import "ChatExtDB.h"

#define tChatExtDB  @"ChatExtDB"
#define nAvatar   @"avatar"
#define nUid      @"uid"
#define nNickname @"nickname"
#define nUsername @"username"

@implementation ChatExtDB

- (instancetype)init {
  self = [super init];
  if (self) {
    self.avatar   = @"";
    self.uid      = @"";
    self.nickname = @"";
    self.username = @"";
  }
  return self;
}

- (instancetype)initWithDBSet:(FMResultSet *)set {
  self = [super initWithDBSet:set];
  if (set) {
    self.avatar   = [set stringForColumn:nAvatar];
    self.uid      = [set stringForColumn:nUid];
    self.nickname = [set stringForColumn:nNickname];
    self.username = [set stringForColumn:nUsername];
  }
  return self;
}

- (instancetype)initWithMode:(Ext *)group {
  self = [super init];
  if (group) {
    self.avatar   = group.avatar;
    self.uid      = group.uid;
    self.nickname = group.nickname;
    self.username = group.username;
  }
  return self;
}

+ (BOOL)creatTable {
  FMDatabase *db = [self database];

  [self open];
  NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL, '%@' TEXT, '%@' TEXT, '%@' TEXT)", tChatExtDB, nUid, nAvatar, nNickname, nUsername];
  
  BOOL res = [db executeUpdate:sql];
  
  [self close];
  
  return res;
}

- (BOOL)insert {
  NSString *sql = [NSString stringWithFormat:@"insert into \
                   %@ (%@, %@, %@, %@) \
                   values (?, ?, ?, ?)",
                   tChatExtDB, nUid, nAvatar, nNickname,
                   nUsername];
  [self open];
  
  BOOL result = [self.database executeUpdate:sql
                        withArgumentsInArray:@[ self.uid, self.avatar, self.nickname, self.username]];

  [self close];
  
  return result;
}

- (BOOL)modify {
  NSString *sql = [NSString stringWithFormat:@"update %@ set %@=?, %@=?, %@=? where %@ = ?",
                   tChatExtDB,nUsername , nAvatar,
                   nNickname, nUid];
  [self open];
  
  BOOL result = [self.database executeUpdate:sql
                        withArgumentsInArray:@[self.username, self.avatar, self.nickname,self.uid ]];
  
  [self close];
  
  return result;
}


+ (BOOL)deleteAll {
  NSString *sql = [NSString stringWithFormat:@"delete from %@", tChatExtDB];

  [self open];
  
  BOOL result = [self.database executeUpdate:sql];
  
  [self close];
  
  return result;
}

+ (ChatExtDB *)queryExtByID:(NSString *)Uid{
  
  ChatExtDB *ChatExtDB = nil;
  
  NSString *sql = [NSString stringWithFormat:@"select * from %@ where %@ == ?", tChatExtDB,nUid];
  
  [self open];
  
  FMResultSet *set = [self.database executeQuery:sql, Uid];
  
  if ([set next]) {
    ChatExtDB = [[self alloc] initWithDBSet:set];
  }
  
  [self close];
  
  return ChatExtDB;
}

+ (ChatExtDB *)queryExtByUserName:(NSString *)UserName{
  
  ChatExtDB *ChatExtDB = nil;
  
  NSString *sql = [NSString stringWithFormat:@"select * from %@ where %@ == ?", tChatExtDB,nUsername];
  
  [self open];
  
  FMResultSet *set = [self.database executeQuery:sql, UserName];
  
  if ([set next]) {
    ChatExtDB = [[self alloc] initWithDBSet:set];
  }
  
  [self close];
  
  return ChatExtDB;
}

+ (NSArray *)queryAllExt {
  NSString *sql = [NSString stringWithFormat: @"select * from %@",tChatExtDB ];
  
  [self open];
  
  FMResultSet *set = [self.database executeQuery:sql];
  NSArray *array = [self arrayWithDBSet:set];
  
  [self close];
  
  return array;
}

@end
